package org.ru.mse10.cvis.web.helper;

import java.util.ArrayList;
import java.util.List;

import org.ru.mse10.cvis.util.CommonUtil;
import org.ru.mse10.cvis.web.model.UIModel;
import org.ru.mse10.cvis.web.model.initializer.NewInstanceInitializer;

/**
 * Base "helper" class for tables bound to a {@link UIModel} for rows.
 * 
 * @param <E>
 *            Type of the model object.
 * @author yasko
 */
public class ModelTableHelper<E> {

	/** The new instance initializer. */
	private NewInstanceInitializer<E> newInstanceInitializer;

	/** The rows. */
	private List<UIModel<E>> rows = new ArrayList<UIModel<E>>();

	/** The current index. */
	private int currentIndex = 0;

	/** The current row. */
	private UIModel<E> currentRow;

	/**
	 * Constructs new instance of the helper.
	 * 
	 * @param initializer
	 *            Initializer for new rows.
	 */
	public ModelTableHelper(NewInstanceInitializer<E> initializer) {
		this.newInstanceInitializer = initializer;
		this.currentRow = new UIModel<E>(newInstanceInitializer.newInstance());
	}

	/**
	 * Initializes the helper with the specified list of model objects.
	 * 
	 * @param modelObjects
	 *            Model objects.
	 */
	public void reset(List<E> modelObjects) {
		setCurrentIndex(0);
		getRows().clear();
		for (E e : modelObjects) {
			UIModel<E> ui = new UIModel<E>(e);
			getRows().add(ui);
		}
	}

	/**
	 * Inits the new row.
	 */
	public void initNewRow() {
		currentRow = new UIModel<E>(getNewInstanceInitializer().newInstance());
	}

	/**
	 * Store.
	 */
	public void store() {
		rows.set(currentIndex, currentRow);
	}

	/**
	 * Store new row.
	 */
	public void storeNewRow() {
		rows.add(0, currentRow);
	}

	/**
	 * Removes the.
	 */
	public void remove() {
		rows.remove(rows.get(currentIndex));
	}

	/**
	 * Gets the new instance initializer.
	 * 
	 * @return the new instance initializer
	 */
	public NewInstanceInitializer<E> getNewInstanceInitializer() {
		return newInstanceInitializer;
	}

	/**
	 * Sets the new instance initializer.
	 * 
	 * @param newInstanceInitializer
	 *            the new new instance initializer
	 */
	public void setNewInstanceInitializer(
			NewInstanceInitializer<E> newInstanceInitializer) {
		this.newInstanceInitializer = newInstanceInitializer;
	}

	/**
	 * Gets the rows.
	 * 
	 * @return the rows
	 */
	public List<UIModel<E>> getRows() {
		return rows;
	}

	/**
	 * Sets the rows.
	 * 
	 * @param rows
	 *            the new rows
	 */
	public void setRows(List<UIModel<E>> rows) {
		this.rows = rows;
	}

	/**
	 * Gets the current index.
	 * 
	 * @return the current index
	 */
	public int getCurrentIndex() {
		return currentIndex;
	}

	/**
	 * Sets the current index.
	 * 
	 * @param currentIndex
	 *            the new current index
	 */
	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}

	/**
	 * Gets the current row.
	 * 
	 * @return the current row
	 */
	public UIModel<E> getCurrentRow() {
		return currentRow;
	}

	/**
	 * Sets the current row.
	 * 
	 * @param currentRow
	 *            the new current row
	 */
	public void setCurrentRow(UIModel<E> currentRow) {
		this.currentRow = currentRow;
	}

	/**
	 * Push to.
	 * 
	 * @param educationEntries
	 *            the education entries
	 */
	public void pushTo(List<E> educationEntries) {
		educationEntries.clear();
		for (UIModel<E> row : getRows()) {
			educationEntries.add(row.getModelObject());
		}
	}

	/**
	 * Checks if is empty model.
	 * 
	 * @return true, if is empty model
	 */
	public boolean isEmptyModel() {
		return CommonUtil.isEmpty(getRows());
	}
}
